package task;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.eclipse.swt.graphics.ImageData;

import util.JdbcUtil;


public class RunSql extends DbTask {
	@Override
	protected void doRun(Map<String, String> props) throws SQLException {
		String sql = props.get("sql");
		
		new JdbcUtil.Traverser(conn(), sql) {
			private int n;
			@Override
			protected void visit(ResultSet rs) throws SQLException {
				for (int i=0 ; i<n ; i++) {
					System.out.printf("'%s'\t", rs.getString(i+1));
				}
			}
			@Override
			protected void prepare(ResultSet rs) throws SQLException {
				n = rs.getMetaData().getColumnCount();
			}
		};
	}


	@Override
	public String getDescription() {
		String desc = "Run arbitrary sql.";
		return desc;
	}

	@Override
	public ImageData getImageData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] propertyNames() {
		String[] props = { "sql" };
		return props;
	}
}
